import 'dart:developer';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:toatool/src/data/local/storage_preferences.dart';
import 'package:toatool/src/utils/platform_utils.dart';

class HeaderInterceptors extends Interceptor {
  @override
  void onRequest(
      RequestOptions options, RequestInterceptorHandler handler) async {
    options.connectTimeout = 30000;
    options.receiveTimeout = 30000;

    if (!kIsWeb) {
      var appVerisonName = await PlatformUtils.getAppVersion();
      var appVerisonCode = await PlatformUtils.getBuildNum();
      var platform = Platform.operatingSystem;
      late String platformNum;
      if (platform == 'ios') {
        platformNum = 'iOS';
      } else if (platform == 'android') {
        platformNum = 'Android';
      }
      var version = <String, dynamic>{}..addAll(
          <String, dynamic>{
            'platform': platformNum,
            'appVerisonName': appVerisonName,
            'appVerisonCode': appVerisonCode,
            'briefIgnorVersionCode': 1,
          },
        );
      options.headers['version'] = version;
    }
    options.headers['Accept-Language'] = 'ja';
    options.headers['token'] = StoragePreferences.getToken() ?? '';
    log('---api-request--->url-->${options.baseUrl}${options.path} version:${options.headers['version']} token: ${options.headers['token']} queryParameters: ${options.queryParameters} data: ${options.data}');
    super.onRequest(options, handler);
  }
}
